package cn.newgrand.netcall.integration.menu;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jivesoftware.Spark;
import org.jivesoftware.spark.util.StringUtils;
import org.jivesoftware.spark.util.log.Log;

import cn.newgrand.common.StringUtil;
import cn.newgrand.netcall.integration.AppSysRes;
import cn.newgrand.netcall.personal.contact.PersonalContactDef;
import cn.newgrand.netcall.personal.contact.entity.PersonalGroup;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-11-4 下午08:34:30
 * 类说明
 */
public class AppSysMenuHandler {
	
	private Map<String,AppSysItem> itemMap = new HashMap<String, AppSysItem>();
	private Map<String,AppSysItemGroup> itemGroupMap = new HashMap<String, AppSysItemGroup>();
//	private static File resourceDirectory = new File(Spark.getResourceDirectory(),"integration.xml");
	private static File resourceDirectory = new File(Spark.getResourceDirectory(),"integration.xml");
//	private static File resourceDirectory = new File("D:\\", "integration.xml");
	String menuFuncXML = null;
	private Map<String,AppSysItemGroup> groupMap = new HashMap<String, AppSysItemGroup>();
	private AppSysItemGroup funcMenuGroup = new AppSysItemGroup("快捷功能菜单");
	private Collection<AppSysItem> funcMenuItemList = new ArrayList<AppSysItem>();
	
	public AppSysMenuHandler(){
	}
	
	public void loadMenu(){
		
		menuFuncXML = AppSysRes.getPropertyOnServer(AppSysRes.APP_USER_MENU_FUNC);
		if(!StringUtil.isEmpty(menuFuncXML)){
			menuFuncXML = StringUtils.unescapeFromXML(menuFuncXML);
			menuFuncXML = StringUtils.replace(menuFuncXML,"&","&amp;");
			SAXReader reader = new SAXReader();
			Document document = null ;
			try {
				document = reader.read(new ByteArrayInputStream(menuFuncXML.getBytes("utf-8")));
			} catch (UnsupportedEncodingException e) {
				Log.error(e.getMessage(),e);
				return;
			} catch (DocumentException e) {
				Log.error(e.getMessage(),e);
				return;
			}
			
			if(null==document) return ;
			if(!document.hasContent())return;
			Element menu = document.getRootElement();
			//为了解决区分大小写的问题
			Iterator<Element> items = menu.elementIterator("Function");
			if(!items.hasNext()){items = menu.elementIterator("function");}
					while(items.hasNext()){
						String id = null;
						String name = null;
						String url = null;
						String pid = null;
						Element  item = items.next();
						Iterator<Element> funcElements = item.elementIterator();
						while (funcElements.hasNext()) {
							Element funcElement = funcElements.next();
							String nameElement = funcElement.getName().toLowerCase();
							if ("funcno".equals(nameElement)) {
								id = funcElement.getText();
								if(StringUtil.isEmpty(id)){
									break;
								}
							} else if ("description".equals(nameElement)) {
								name = funcElement.getText();
							} else if ("funcurl".equals(nameElement)) {
								url = funcElement.getText();
//								url = StringUtils.replace(funcElement.getText(),"&","&amp;");
							}
						}
						AppSysItem appSysItem = new AppSysItem(id, name, url,pid);
						funcMenuItemList.add(appSysItem);
//						itemMap.put(id,appSysItem);
//						funcMenuGroup.addAppSysItem(appSysItem);
						
					}
			}
//		itemGroupMap.put("0001", funcMenuGroup);
		}
	public void loadStringMenu(){
		InputStream inputStream;
		StringBuffer buffer;
		BufferedReader bufferReader;
		try {
			inputStream = new FileInputStream(resourceDirectory);
		
			buffer = new StringBuffer();
			String line; // 用来保存每行读取的内容
			bufferReader = new BufferedReader(new InputStreamReader(
					inputStream));
			line = bufferReader.readLine(); // 读取第一行
			while (line != null) { // 如果 line 为空说明读完了
			buffer.append(line); // 将读到的内容添加到 buffer 中
			// buffer.append("\n"); // 添加换行符
			line = bufferReader.readLine(); // 读取下一行
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.error(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.error(e.getMessage());
		}
		
		// 将读到 buffer 中的内容写出来
//		inputstream.close();
//		filetext = buffer.toString();
//		filetext就是得到的字符串
	}
	
	public void buildFourMenu(){
		AppSysItemGroup tempGroup = new AppSysItemGroup("快捷菜单");
		itemGroupMap.put("0001", tempGroup);
		
//		AppSysItem item1 = new AppSysItem("000001", "科目余额表","exe:[I6][INTFI]GC:01:201040301","pid");
//		AppSysItem item2 = new AppSysItem("000002", "管理驾驶舱","exe:[i6][I6EPM]MC:13201:?DepInst=EPM.MC&amp;DepModule=130", "pid");
//		AppSysItem item3 = new AppSysItem("000003", "卡片列表","exe:[I6][i6EAM]AC:02:2050201", "pid");
//		AppSysItem item4 = new AppSysItem("000004", "员工档案列表","HR/Hr.Presentation.Emp/EmpListPage.aspx", "pid");
		AppSysItem item1 = new AppSysItem("000001", "待办审批任务","Hr/W3.Presentation.AppFlow/AppFlowListPage.aspx","pid");
		AppSysItem item2 = new AppSysItem("000002", "我的送审列表","Hr/W3.Presentation.AppFlow/AppFileListPage.aspx?QueryType=SendList", "pid");
		AppSysItem item3 = new AppSysItem("000003", "待审批列表","exe||AppFlowTaskListManager", "pid");
		AppSysItem item4 = new AppSysItem("000004", "我的待办任务","WORKFLOW/NG.WorkFlow.Web/WorkFlowManager.aspx", "pid");
		AppSysItem item5 = new AppSysItem("000005", "业务工作流列表","exe:[I6P][psoftENV]WF:01:60", "pid");
		
		tempGroup.addAppSysItem(item1);
		tempGroup.addAppSysItem(item2);
		tempGroup.addAppSysItem(item3);
		tempGroup.addAppSysItem(item4);
		tempGroup.addAppSysItem(item5);
	}
	
	public Map<String,AppSysItemGroup> getAppSysItemGroup(){
		return itemGroupMap;
	}
	
	public Map<String,AppSysItem> getAppSysItem(){
		return itemMap;
	}
	
	public Collection<AppSysItem> getFuncMenuList(){
		return funcMenuItemList;
	}
	
	public static void main(String[] args ){
		AppSysMenuHandler as = new AppSysMenuHandler();
		as.loadMenu();
		for (AppSysItemGroup itemGroup : as.getAppSysItemGroup().values()) {
			System.out.println(itemGroup.getName());
			for (AppSysItem item : itemGroup.getAppSysItems()) {
				System.out.println("\t"+item.getName());
			}
		}
	}
	
//public void loadMenu(){
//		
//		menuFuncXML = AppSysRes.getPropertyOnServer(AppSysRes.APP_USER_MENU_FUNC);
//		if(!StringUtil.isEmpty(menuFuncXML)){
//			
//			SAXReader reader = new SAXReader();
//			Document document = null ;
//			try {
//				document = reader.read(new ByteArrayInputStream(menuFuncXML.getBytes("UTF-8")));
//			} catch (UnsupportedEncodingException e) {
//				Log.error(e.getMessage(),e);
//				return;
//			} catch (DocumentException e) {
//				Log.error(e.getMessage(),e);
//				return;
//			}
//			
//			if(null==document) return ;
//			if(!document.hasContent())return;
//			Element menu = document.getRootElement();
//			//为了解决区分大小写的问题
//			Iterator<Element> items = menu.elementIterator("Function");
//			if(!items.hasNext()){items = menu.elementIterator("function");}
//					while(items.hasNext()){
//						String id = null;
//						String name = null;
//						String url = null;
//						String pid = null;
//						Element  item = items.next();
//						List<Attribute> itemAttrList =  item.attributes();
//						for(Attribute attr : itemAttrList){
//							if(attr.getName().equals("funcno")){
//								id = attr.getText();
//							}else
//							if(attr.getName().equals("PID")){
//								pid = attr.getText();
//							}else
//							if(attr.getName().equals("str")){
//								name = attr.getText();
//							}else
//							if(attr.getName().equals("url")){
//								url = attr.getText();
//							}
//							
//						}
//						AppSysItem appSysItem = new AppSysItem(id, name, url,pid);
//						itemMap.put(id,appSysItem);
//						if(StringUtil.isEmpty(pid)&& null== itemGroupMap.get(id)) {
//							itemGroupMap.put(id,new AppSysItemGroup(name));
//						} else if(!StringUtil.isEmpty(pid)&& null==itemGroupMap.get(pid)){
//							AppSysItemGroup tempGroup = new AppSysItemGroup("");
//							itemGroupMap.put(pid,tempGroup);
//							tempGroup.addAppSysItem(appSysItem);
//						}else if(!StringUtil.isEmpty(pid)&& null!=itemGroupMap.get(pid)){
//							itemGroupMap.get(pid).addAppSysItem(appSysItem);
//						}
//						
//					}
//					for (AppSysItem item : itemMap.values()) {
//						if(null!=itemGroupMap.get(item.getID())){
//							itemGroupMap.get(item.getID()).setGroupName(item.getName());
//						}
//					}
//			}
//		}
}
